package com.dog.basics.service.common.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.common.PositioninfoAddDto;
import com.dog.basics.bean.dto.admin.common.PositioninfoEditDto;
import com.dog.basics.bean.dto.admin.common.PositioninfoFormDto;
import com.dog.basics.bean.model.common.DogComPositioninfo;
import com.dog.basics.bean.vo.admin.common.PositioninfoVo;
import com.dog.basics.bean.vo.api.common.AllPositioninfoVo;
import com.dog.basics.common.util.Json;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.common.DogComPositioninfoMapper;
import com.dog.basics.service.common.IDogComPositioninfoService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 省市区信息 服务实现类
 * </p>
 *
 * @author zmr
 * @since 2020-05-27
 */
@Service
@AllArgsConstructor
public class DogComPositioninfoServiceImpl extends ServiceImpl<DogComPositioninfoMapper, DogComPositioninfo> implements IDogComPositioninfoService {

    /**
     * @Description: 增加省市区信息
     * @Param: PositioninfoAddDto
     * @return: Boolean
     * @Author: zhangrm
     * @Date: 2020/05/27 0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveObj(PositioninfoAddDto obj) {
        DogComPositioninfo dogComPositioninfo = new DogComPositioninfo();
        BeanUtil.copyProperties(obj, dogComPositioninfo);
        Validator.validateTrue(ObjectUtil.isNull(getById(obj.getAreaId())),"省市区标识已经存在!");
        Validator.validateTrue(Validator.equal(obj.getParentId(),0)||ObjectUtil.isNotNull(getById(obj.getParentId()))," 父类省市区标识不存在!");
        //获取父级等全称
        String fullName=getfullName(dogComPositioninfo);
        fullName=fullName.substring(0,fullName.length()-1);
        dogComPositioninfo.setFullName(fullName);
        Validator.validateTrue(this.save(dogComPositioninfo), "增加省市区信息失败!");
        return Boolean.TRUE;
    }

    /**
     * 递归查询全称
     * @param
     * @return
     */
    private String getfullName(DogComPositioninfo obj ){
        if (!Validator.equal(obj.getParentId(),0)){
            DogComPositioninfo dogComPositioninfo=  getById(obj.getParentId());
            return getfullName(dogComPositioninfo)+obj.getAreaName()+',';
        }else {
            return "中国,";
        }
    }

    /**
     * @Description: 删除省市区信息
     * @Param: id
     * @return: Boolean
     * @Author: zhangrm
     * @Date: 2020/05/27 0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteObj(Long id) {
        DogComPositioninfo dogComPositioninfo = this.getById(id);
        Validator.validateNotNull(dogComPositioninfo, "无此数据");
        //区域下有子区域无法删除
        Validator.validateTrue( getBaseMapper().selectCount(Wrappers.<DogComPositioninfo>lambdaQuery().eq(DogComPositioninfo::getParentId,id))==0,"区域下有子区域无法删除!");
        return this.removeById(id);
    }

    /**
     * @Description: 修改省市区信息
     * @Param: PositioninfoEditDto
     * @return: Boolean
     * @Author: zhangrm
     * @Date: 2020/05/27 0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editObj(PositioninfoEditDto obj) {
        DogComPositioninfo dogComPositioninfo = this.getById(obj.getAreaId());
        Validator.validateNotNull(dogComPositioninfo, "无此数据");
        if(Validator.isNotNull(obj.getParentId())){
            //如果修改了父类id
            Validator.validateTrue(Validator.equal(obj.getParentId(),0)||ObjectUtil.isNotNull(getById(obj.getParentId()))," 父类省市区标识不存在!");
        }
        BeanUtil.copyProperties(obj, dogComPositioninfo);
        //获取父级等全称
        String fullName=getfullName(dogComPositioninfo);
        fullName=fullName.substring(0,fullName.length()-1);
        dogComPositioninfo.setFullName(fullName);
        dogComPositioninfo.setUpdateTime(DateUtil.currentSeconds());
        return this.updateById(dogComPositioninfo);
    }

    /**
     * @Description: 分页省市区信息列表
     * @Param: PositioninfoFormDto
     * @return: IPage<PcPositioninfoVo>
     * @Author: zhangmr
     * @Date:  2020/05/27 0027
     */
    @Override
    public IPage<PositioninfoVo> getPageList(PageParam page, PositioninfoFormDto obj) {

     IPage iPage = this.page(page, Wrappers.<DogComPositioninfo>lambdaQuery()
             .eq(ObjectUtil.isNotNull(obj.getAreaId()), DogComPositioninfo::getAreaId, obj.getAreaId())
             .eq(ObjectUtil.isNotNull(obj.getParentId()), DogComPositioninfo::getParentId, obj.getParentId())
             .like(StrUtil.isNotBlank(obj.getAreaName()), DogComPositioninfo::getAreaName, obj.getAreaName())
             .like(StrUtil.isNotBlank(obj.getPingYin()), DogComPositioninfo::getPingYin, obj.getPingYin())
             .like(StrUtil.isNotBlank(obj.getPostCode()), DogComPositioninfo::getPostCode, obj.getPostCode())
             .like(StrUtil.isNotBlank(obj.getTrunkCode()), DogComPositioninfo::getTrunkCode, obj.getTrunkCode())
             .like(StrUtil.isNotBlank(obj.getInitials()), DogComPositioninfo::getInitials, obj.getInitials())
             .eq(ObjectUtil.isNotNull(obj.getLevel()), DogComPositioninfo::getLevel, obj.getLevel())
             .like(StrUtil.isNotBlank(obj.getAreaCode()), DogComPositioninfo::getAreaCode, obj.getAreaCode())
             .eq(StrUtil.isNotBlank(obj.getAreaType()), DogComPositioninfo::getAreaType, obj.getAreaType())
             .eq(ObjectUtil.isNotNull(obj.getIsUse()), DogComPositioninfo::getIsUse, obj.getIsUse()));
        List<DogComPositioninfo> records = iPage.getRecords();
        iPage.setRecords(records.stream().map(e -> {
            PositioninfoVo positioninfoVo = new PositioninfoVo();
            BeanUtil.copyProperties(e, positioninfoVo);
            return positioninfoVo;
        }).collect(Collectors.toList()));
        return iPage;
    }

    /**
     * @Description: 全国省市区信息
     * @Param:
     * @return:
     * @Author: zhangmr
     * @Date:
     */
    @Override
    public AllPositioninfoVo getAllPositioninfo() {
        AllPositioninfoVo allPositioninfoVo = new AllPositioninfoVo();
        allPositioninfoVo.setAreaId(0L);
        getAllPositioninfo(allPositioninfoVo);
        return allPositioninfoVo;
    }



    private  void getAllPositioninfo(AllPositioninfoVo allPositioninfoVo){
        //查询子节点信息
        Long areaId  =  allPositioninfoVo.getAreaId();
        List<DogComPositioninfo> dogComPositioninfoList =  getBaseMapper().selectList(Wrappers.<DogComPositioninfo>lambdaQuery().eq(DogComPositioninfo::getParentId,areaId).eq(DogComPositioninfo::getIsUse,0) );
       if (CollUtil.isNotEmpty(dogComPositioninfoList)){
           List<AllPositioninfoVo>  allPositioninfoVoList = dogComPositioninfoList.stream().map(e->{
               AllPositioninfoVo vo = new AllPositioninfoVo();
               BeanUtil.copyProperties(e, vo);
               return vo;
           }).collect(Collectors.toList());
           allPositioninfoVo.setAllPositioninfoVoList(allPositioninfoVoList);
           for (AllPositioninfoVo cAllPositioninfoVo:allPositioninfoVoList) {
               //递归
               getAllPositioninfo(cAllPositioninfoVo);
           }
       }
    }

    /**
     * @Description: 通过父ID获取子省市区信息
     * @Param:
     * @return:
     * @Author: zhangmr
     * @Date:
     */
    @Override
    public List<com.dog.basics.bean.vo.api.common.PositioninfoVo> getPositioninfo(Long id) {
        //查询子节点信息
        List<DogComPositioninfo> dogComPositioninfoList =  getBaseMapper().selectList(Wrappers.<DogComPositioninfo>lambdaQuery().eq(DogComPositioninfo::getParentId,id) .eq(DogComPositioninfo::getIsUse,0));
        Validator.validateTrue(CollUtil.isNotEmpty(dogComPositioninfoList),"此节点下无子节点！");
        List<com.dog.basics.bean.vo.api.common.PositioninfoVo>  positioninfoVoList = dogComPositioninfoList.stream().map(e->{
            com.dog.basics.bean.vo.api.common.PositioninfoVo vo = new com.dog.basics.bean.vo.api.common.PositioninfoVo();
            BeanUtil.copyProperties(e, vo);
            return vo;
        }).collect(Collectors.toList());
        return  positioninfoVoList;
    }

    /**
     * @Description: 设置省市区信息是否可用
     * @Param: PositioninfoEditDto
     * @return: Boolean
     * @Author: zhangrm
     * @Date: 2020/05/27 0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editObjUse(Long id) {
        DogComPositioninfo dogComPositioninfo = this.getById(id);
        Validator.validateNotNull(dogComPositioninfo, "无此数据");
        dogComPositioninfo.setIsUse(dogComPositioninfo.getIsUse()==0?1:0);
        return this.updateById(dogComPositioninfo);
    }
}
